<template>
	<div class="view">
		 <Modal 
        		v-model="data.isShow"
        		width="400">
        		<p slot="header">
	            <span>{{data.actionType==1?'新建':'编辑'}}管理员</span>
	        </p>
	        
			<Form ref="form" 
				:model="data.data" 
				:rules="ruleValidate" 
				:label-width="80">
				
		        <Form-item 
		        		label="账户名" 
		        		prop="username" 
		        		style="width: 300px;">
		            <Input type="text" v-model="data.data.username"></Input>
		        </Form-item>
		        
				<Form-item 
		        		label="手机号" 
		        		prop="phoneNum" 
		        		style="width: 300px;">
		            <Input type="text" v-model="data.data.phoneNum"></Input>
		        </Form-item>
		        
				<Form-item 
		        		label="姓名" 
		        		prop="name" 
		        		style="width: 300px;">
		            <Input type="text" v-model="data.data.name"></Input>
		        </Form-item>
		        
				<Form-item 
		        		label="角色" 
		        		prop="roleId" 
		        		style="width: 300px;">
					<Select v-model="data.data.roleType" style="width:220px">
       					<Option v-for="(item, index) in roleTypes" 
       						:value="item.value" 
       						:key="index" 
       						:label="item.label">
       					</Option>
					</Select>
		       </Form-item>
		       
		    </Form>
		    <div slot="footer">
	           	<Button type="primary" 
	           		:loading="data.isSubmit" 
	           		@click="handleSubmit('form')">
	           		确定
	           	</Button>
	           	
	            <Button type="ghost" 
	            		:disabled="data.isSubmit" 
	            		@click="handleCancel('form')" 
	            		style="margin-left: 8px">关闭</Button>
	        </div>
	     </Modal>
	</div>

</template>

<script>
	export default {
		props: ['data'],
		data() {
			// 验证手机号
			const validatorPhone = (rule, val, callback) => {
				var errors = [];
				if (!/^1\d{10}$/.test(val)) {
					callback('手机格式不正确');
				} 
				callback(errors)
			};
			return {
				roleTypes: [
					{
						value: 1,
						label: "超级管理员"
					}, {
						value: 2,
						label: "管理员"
					}, {
						value: 3,
						label: "审核员"
					}, {
						value: 4,
						label: "录入员"
					}
				],
				ruleValidate: {
                    username: [
                        { required: true, message: '账号不能为空', trigger: 'blur' }
					],
					phoneNum: [
						{ required: true, message: '手机号不能为空', trigger: 'blur' },
                        { validator: validatorPhone, trigger: 'blur'}
                    ],
					name: [
						{ required: true, message: '姓名不能为空', trigger: 'blur' }
                    ]
				}
			}
		},
		mounted: function() {
		},
		methods: {
			handleSubmit (name) {
				var $this = this;
                this.$refs[name].validate((valid) => {
                    if (valid) {
                    		this.$emit('on-submit', $this.data);
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                });
            },
            handleCancel (name) {
              	this.$emit('on-cancel');
              	this.data.isShow = false;
            }
		}
	}
</script>

<style scoped="scoped">
	.view {
		padding: 20px;
	}
</style>